/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cuong.bean;

import client.entity.Category;
import client.entity.Customer;
import client.entity.JobSubject;
import client.entity.LevelJob;
import client.entity.Location;
import client.entity.Recruitment;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import util.HibernateUtil;

/**
 *
 * @author Admin Windows 8
 */
@ManagedBean
@SessionScoped
public class LoadJobByIdCategoryBean {

    /**
     * Creates a new instance of LoadJobByIdCategoryBeanBK
     */
    private static final int DEFAULT_RECORDS_NUMBER = 4;
    private List<JobSubject> list;
    public int id = -1;
    private Map<String, String> params;
    private List<Recruitment> listRecruiment;
    private Recruitment recruiment;
    private Category category;
    private Location location;
    private int count = 0;
    private int trang = 1;
    private int tongtrang;

    public LoadJobByIdCategoryBean() {
    }

    public void next() {
        if (trang < tongtrang) {
            trang++;
            count++;
        }
        getList();
    }

    public void prev() {
        if (this.trang > 1) {
            count--;
            trang--;
        }
    }

    public void first() {
        trang = 1;
        count = 0;

    }

    public void last() {
        trang = tongtrang;
        count = trang - 1;
    }

    public int getTrang() {
        return trang;
    }

    public void setTrang(int trang) {
        this.trang = trang;
    }

    public int getTongtrang() {
        int totalRc = getSizeDataByID();
        tongtrang = totalRc / DEFAULT_RECORDS_NUMBER;
        if (totalRc % DEFAULT_RECORDS_NUMBER > 0) {
            tongtrang++;
        }
        return tongtrang;
    }

    public void setTongtrang(int tongtrang) {
        this.tongtrang = tongtrang;
    }

    public List<JobSubject> getList() {
        list = new ArrayList<JobSubject>();
        List<Recruitment> listRe = getListRecruiment();
        for (Recruitment recruitment : listRe) {
            JobSubject js = new JobSubject();
            js.setId(recruitment.getId());
            js.setTitle(recruitment.getTitle());
            if (recruitment.getTypeOfJob() == true) {
                js.setTypejob("Full Time");
            } else {
                js.setTypejob("Pass Time");
            }
            js.setNamecustomer(getCustomerByID(recruitment.getCustomer().getId()).getName());
            js.setNameleveljob(getLevelJobByID(recruitment.getLevelJob().getId()).getName());

            List<Category> listCate = getListCateByIDRecrui(recruitment.getId());
            StringBuffer stringCate = new StringBuffer();
            for (int i = 0; i < listCate.size(); i++) {
                Category ct = listCate.get(i);
                if (i < listCate.size() - 1) {
                    stringCate.append(ct.getCategoryName() + ", ");
                } else {
                    stringCate.append(ct.getCategoryName());
                }
            }
            js.setNamecate(stringCate.toString());

            List<Location> listLoca = getListLocaByIDRecrui(recruitment.getId());
            StringBuffer stringLocale = new StringBuffer();
            for (int i = 0; i < listLoca.size(); i++) {
                Location lc = listLoca.get(i);
                if (i < listLoca.size() - 1) {
                    stringLocale.append(lc.getName() + ", ");
                } else {
                    stringLocale.append(lc.getName());
                }
            }
            js.setNamelocation(stringLocale.toString());
            list.add(js);
        }
        return list;
    }

    public List<Recruitment> getListRecruiment() {
        String pr = getParam("nameCategory");
        if (pr == null) {
            id = (Integer) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("cateid");
        } else {
            id = getIdCAteByName(getParam("nameCategory"));
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("cateid", id);
        }
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session ss = factory.openSession();
        ss.getTransaction().begin();
        listRecruiment = ss.createQuery("select re from Recruitment as re inner join re.recruitmentCategories as rc where rc.category.id=" + id)
                .setFirstResult(count * DEFAULT_RECORDS_NUMBER).setMaxResults(DEFAULT_RECORDS_NUMBER).list();
        ss.getTransaction().commit();
        ss.close();
        return listRecruiment;
    }

    public List<Category> getListCateByIDRecrui(int idRecrui) {
        List<Category> list = new ArrayList<Category>();
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session ss = factory.openSession();
        ss.getTransaction().begin();
        list = ss.createQuery("select c from Category as c inner join c.recruitmentCategories as rc where rc.recruitment.id=" + idRecrui).list();
        ss.getTransaction().commit();
        ss.close();
        return list;
    }

    public List<Location> getListLocaByIDRecrui(int idRecrui) {
        List<Location> list = new ArrayList<Location>();
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session ss = factory.openSession();
        ss.getTransaction().begin();
        list = ss.createQuery("select lo from Location as lo inner join lo.recruitmentLocations as rl where rl.recruitment.id=" + idRecrui).list();
        ss.getTransaction().commit();
        ss.close();
        return list;
    }

    public LevelJob getLevelJobByID(int id) {
        LevelJob lj = new LevelJob();
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session ss = factory.openSession();
        ss.getTransaction().begin();
        lj = (LevelJob) ss.createQuery("from LevelJob lj where lj.id=" + id).uniqueResult();
        ss.getTransaction().commit();
        ss.close();
        return lj;
    }

    public Customer getCustomerByID(int id) {
        Customer lj = new Customer();
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session ss = factory.openSession();
        ss.getTransaction().begin();
        lj = (Customer) ss.createQuery("from Customer cus where cus.id=" + id).uniqueResult();
        ss.getTransaction().commit();
        ss.close();
        return lj;
    }

    public String getParam(String param) {
        Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        return params.get(param);
    }

    public int getIdCAteByName(String name) {
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session ss = factory.openSession();
        ss.getTransaction().begin();
        id = (Integer) ss.createSQLQuery("select ID from Category where Category.Category_Name='" + name + "'").addScalar("ID", Hibernate.INTEGER).uniqueResult();
        ss.getTransaction().commit();
        ss.close();
        return id;
    }

    public int getSizeDataByID() {//5
        String pr = getParam("nameCategory");
        if (pr == null) {
            id = (Integer) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("cateid");
        } else {
            id = getIdCAteByName(getParam("nameCategory"));
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("cateid", id);
        }
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session ss = factory.openSession();
        ss.getTransaction().begin();
        Object qtt = ss.createSQLQuery("select COUNT(Recruitment_Category.ID_Recruitment) AS 'Qtt' FROM Recruitment_Category  WHERE Recruitment_Category.ID_Category=" + id)
                .addScalar("Qtt", Hibernate.INTEGER).uniqueResult();
        ss.getTransaction().commit();
        ss.close();
        return (Integer) qtt;
    }
}
